package cs423.loadBalancer.core;

import java.nio.ByteBuffer;

/**
 * 
 * @author Bobby Chen
 *	Job object
 */
public class Job {

	int width;
	int height;

	int multiplier;

	double[][] matrix;
	double[][] firstHalf;
	double[][] secondHalf;
	byte [] passValue;
	
	/**
	 * constructor
	 * @param width
	 * @param height
	 */
	public Job(int width, int height) {
		this.width = width;
		this.height = height;
		this.multiplier = (int)(Math.random() * 100);
		Main.multiplier = this.multiplier;
		initialize();
	}

	/**
	 * constructor
	 * @param width
	 * @param height
	 * @param multi
	 */
	public Job(int width, int height, int multi) {
		this.width = width;
		this.height = height;
		this.multiplier = multi;
		Main.multiplier = multi;
		initialize();
	}

	/**
	 * initialize the job(matrix)
	 */
	public void initialize() {

		matrix = new double[height][width];
		firstHalf = new double[height / 2][width];
		secondHalf = new double[height - height / 2][width];

		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				double value = (Math.random() * 3.345);
				matrix[i][j] = value;
				if (i < height / 2)
					firstHalf[i][j] = value;
				else
					secondHalf[i - (height / 2)][j] = value;
			}
		}

	}

	//get job
	public double[][] getJob() {
		return matrix.clone();
	}

	public double[][] getFirstHalf() {
		return firstHalf.clone();
	}

	
	public byte[] getFirstHalfByte() {
		byte[] bytes = new byte[firstHalf.length * firstHalf[0].length * 8];
		ByteBuffer buf = ByteBuffer.wrap(bytes);
		for (int i = 0; i < firstHalf.length; ++i) {
			for (double d : firstHalf[i])
				buf.putDouble(d);
		}
		return bytes.clone();
	}

	public double[][] getSecondHalf() {
		return secondHalf.clone();
	}

	
	public byte [] getSecondHalfByte() {
		byte[] bytes = new byte[secondHalf.length * secondHalf[0].length * 8];
		ByteBuffer buf = ByteBuffer.wrap(bytes);
		for (int i = 0; i < secondHalf.length; ++i) {
			for (double d : secondHalf[i])
				buf.putDouble(d);
		}
		return bytes.clone();
	}

	//get whole matrix in byte
	public byte [] getWholeByte(){
		passValue = new byte[matrix.length * matrix[0].length * 8];
		ByteBuffer buf = ByteBuffer.wrap(passValue);
		for (int i = 0; i < matrix.length; ++i) {
			for (double d : matrix[i])
				buf.putDouble(d);
		}
		return passValue.clone();

	}
	
}
